<?php
/**
 * @author Tibor Pino (tibor.pino@gmail.com)
 * @license released under GNU Public License v. 2
 * @copyright Copyright (c) 2007, Tibor Pino
 *
 */

class AppController extends Controller {
    
    public $helpers = array('AdminMenu','Javascript','Time');
    public $uses = array('Setting','Theme');
    public $components = array("RequestHandler",'Cookie','Auth');
    public $view = 'Theme';

    public function beforeFilter() {
        $this->setDefaultSettings();
        $this->setAuthComponent();
        $this->setTheme();

        if(@$this->params[Configure::read('Routing.admin')]) {
            $this->layout = 'admin';
            $this->pageTitle = __('Administration',true);
        }
    }

    private function setDefaultSettings() {
        $defaultSettings = $this->Setting->findById(1);

        Configure::write('Homepage.news',$defaultSettings['Setting']['homepagenews']);
        Configure::write('Homepage.articles',$defaultSettings['Setting']['homepagearticles']);
        Configure::write('Homepage.downloads',$defaultSettings['Setting']['homepagedownloads']);
        Configure::write('Homepage.comments',$defaultSettings['Setting']['homepagecomments']);
        Configure::write('Date.short',$defaultSettings['Setting']['dateshort']);
        Configure::write('Date.long',$defaultSettings['Setting']['datelong']);
        Configure::write('Page.author',$defaultSettings['Setting']['pageauthor']);
        Configure::write('Page.url',$defaultSettings['Setting']['pageurl']);
        Configure::write('Captcha.question',$defaultSettings['Setting']['captchaquestion']);
        Configure::write('Captcha.answer',$defaultSettings['Setting']['captchaanswer']);
        Configure::write('Statistics.displayed.items',3);
        $this->pageTitle = $defaultSettings['Setting']['pagetitle'];
        $feedburner = $defaultSettings['Setting']['feedburner'];
        $googlecse = $defaultSettings['Setting']['googlecse'];
        $this->set(compact('feedburner','googlecse'));
    }

    private function setAuthComponent() {
        $this->Auth->authorize = 'controller';
        if(@$this->params[Configure::read('Routing.admin')]) {
            $this->Auth->loginRedirect = array('controller'=>'statistics','action'=>'index');
        } else {
            $this->Auth->loginRedirect = array('controller' => 'articles', 'action' => 'index');
        }
        $this->Auth->logoutRedirect = '/';
        $this->Auth->allow(array('display','index','view','get','rss','rating','comment_add','comment_preview','login','logout','sidebox','right'));
        $this->Auth->userScope = array('User.active' => 1);
    }

    public function isAuthorized() {
        if (isset($this->params[Configure::read('Routing.admin')])) {
            if ($this->Auth->user('group_id') != 1) {
                return false;
            }
        }
        $user = array('id'=>$this->Auth->user('id'));
        $this->set(compact('user'));
        return true;
    }

    private function setTheme() {
        $theme = $this->Theme->find('Theme.active=1','folder');
        if(empty($theme['Theme']['folder'])) {
            $this->theme = 'default';
        } else {
            $this->theme = $theme['Theme']['folder'];
        }
    }
}

?>